﻿Public Class SP_DatHang
    Dim dhDT As DatHangDT
    Dim dhBL As DatHangBL
    Dim dhctDT As DatHangChiTietDT
    Dim dhctBL As DatHangChiTietBL
    Dim check As New CheckClass
    Dim thanhtien As Double

    Private Sub load_DGV(ByVal _dieukien As String)
        dhBL = New DatHangBL
        Dim list As New List(Of DatHangDT)
        list = dhBL.GetData(_dieukien)
        dgv.DataSource = list
        dgv.ClearSelection()
    End Sub
    Private Sub load_DGVCT(ByVal _dieukienthem As String)
        Dim list As New List(Of DatHangChiTietDT)
        dhctBL = New DatHangChiTietBL
        If dgv.SelectedRows.Count > 0 Then
            list = dhctBL.GetData(" WHERE MaPhieu = '" + dgv.SelectedRows(0).Cells(0).Value.ToString() + "'" + _dieukienthem)
        End If
        dgvCT.DataSource = list
        dgvCT.ClearSelection()
    End Sub
    Private Sub loadCBOX()
        Dim kh As New KhachHangBL
        Dim list As New List(Of KhachHangDT)
        list = kh.GetData("")
        cbKH.DataSource = list
        cbKH.DisplayMember = "TenKH"
        cbKH.ValueMember = "MaKH"
    End Sub
    Private Sub loadCBOXLoai()
        Dim sp As New SanPhamBL
        Dim list As New List(Of SanPhamDT)
        list = sp.GetData("")
        cbLoai.DataSource = list
        cbLoai.DisplayMember = "TenLoai"
        cbLoai.ValueMember = "MaLoai"
    End Sub

    Private Function tinhThanhTien() As Double
        Dim sp As New SanPhamBL
        Dim trigia As Double = sp.getTriGia(cbLoai.SelectedValue)
        thanhtien = Double.Parse(txtSL.Text) * trigia
        Return thanhtien
    End Function

    Private Sub SP_DatHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lbTenNV_Nhap.Text = Main.NVname
        load_DGV("")
        loadCBOX()
        loadCBOXLoai()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        gbTTDH.Enabled = True
        txtSL.Text = ""
        txtGhiChu.Text = ""
        btnThem.Enabled = False
        btnLuu.Enabled = True
        btnOK.Enabled = True
        btnSua.Enabled = False
        btnXoa.Enabled = False
        cbLoai.Enabled = True
        dgv.ClearSelection()
        load_DGVCT("")
        dtpNgayGiao.Enabled = True
        dhBL = New DatHangBL
        Dim so As Integer = dhBL.GetID()
        Dim ma As String = "DH" + so.ToString("00000")
        Dim ngaydat As Date = Date.Today
        'kiem tra trong ngay da co khach hang do dat chua, neu co thi Ghi chu la DAT THEM
        If check.IsExist(" SanPham_DatHang", " WHERE ngayDH = '" + ngaydat + "' AND MaKH = '" + cbKH.SelectedValue + "'") Then
            txtGhiChu.Text = "Đặt thêm"
        End If
        'Tạo phiếu đặt hàng, lấy ngày đặt là now        
        If dtpNgayGiao.Checked = True Then
            dhDT = New DatHangDT(ma, ngaydat, dtpNgayGiao.Value, Main.NVid, cbKH.SelectedValue, "Chưa giao", 0)
        Else
            dhDT = New DatHangDT(ma, ngaydat, Date.Today, Main.NVid, cbKH.SelectedValue, "Chưa giao", 0)
        End If
        dhBL.InPhieuDH(dhDT)
        MessageBox.Show("Phiếu đặt hàng số " + so.ToString() + " đã được tạo. Bắt đầu nhập chi tiết phiếu", "Thành công")
        'load_DGV("") Chi them chua luu nen khong load DGV
        Dim madh As String = dhBL.GetInserted()
        lbMaDH.Text = madh
        lbtenKH.Text = cbKH.Text
        lbThanhtien.Text = 0.ToString()
    End Sub

    Private Sub btnCN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCN.Click
        'cap nhat ngay hen giao
        Dim ngaycu As Date = Convert.ToDateTime(dgv.SelectedRows(0).Cells(4).Value)
        If dtpNgayGiao.Checked = True Then
            dhBL = New DatHangBL
            dhDT = New DatHangDT(dgv.SelectedRows(0).Cells(0).Value.ToString(), dtpNgayGiao.Value, dgv.SelectedRows(0).Cells(2).Value.ToString())
            dhBL.UpNgayHenGiao(dhDT)
            load_DGV("")
            MessageBox.Show("Đã cập nhật lại ngày hẹn giao, ngày hẹn giao cũ :" + ngaycu.ToString("dd/MM/yyyy"), "Thành công")
            dtpNgayGiao.Checked = False
            dtpNgayGiao.Enabled = False
            btnCN.Enabled = False
        End If
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        Dim response As MsgBoxResult = MsgBox("Lưu thông tin và kết thúc nhận đặt hàng?", MsgBoxStyle.YesNo, "Thông báo")
        If response = MsgBoxResult.Yes Then
            gbTTDH.Enabled = False
            btnThem.Enabled = True
            btnLuu.Enabled = False
            txtSL.Text = ""
            txtGhiChu.Text = ""
            dgv.ClearSelection()
            load_DGVCT("")
        Else
            Return
        End If
    End Sub

    Private Sub txtTim_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTim.TextChanged
        If txtTim.Text <> "" Then
            cbKieuTim.Enabled = False
            If cbKieuTim.SelectedItem.ToString() = "Khách hàng" Then
                load_DGV(" WHERE MaKH LIKE '%" + txtTim.Text + "%'")
            ElseIf cbKieuTim.SelectedItem.ToString() = "Nhân viên" Then
                load_DGV(" WHERE MaNV LIKE '%" + txtTim.Text + "%'")
            Else
                load_DGV(" WHERE ngayDH LIKE '%" + txtTim.Text + "%' OR ngayhengiao LIKE '%" + txtTim.Text + "%'")
            End If
        Else
            cbKieuTim.Enabled = True
        End If
    End Sub

    Private Sub txtTim_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTim.KeyDown
        If e.KeyCode = Keys.Enter Then
            If cbKieuTim.SelectedItem.ToString() = "Khách hàng" Then
                load_DGV(" WHERE MaKH LIKE '%" + txtTim.Text + "%'")
            ElseIf cbKieuTim.SelectedItem.ToString() = "Nhân viên" Then
                load_DGV(" WHERE MaNV LIKE '%" + txtTim.Text + "%'")
            Else
                load_DGV(" WHERE ngayDH LIKE '%" + txtTim.Text + "%' OR ngayhengiao LIKE '%" + txtTim.Text + "%'")
            End If
        End If
    End Sub

    Private Sub cbKieuTim_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbKieuTim.SelectedIndexChanged
        If cbKieuTim.Text <> "" Then
            txtTim.Focus()
        End If
    End Sub

    Private Sub llKHKhac_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llKHKhac.LinkClicked
        Dim kh As New KH_Nhap()
        kh.flagIsUp = True
        kh.ShowDialog()
        loadCBOX()
        Dim khBL As New KhachHangBL
        Dim i As String = khBL.GetInserted()
        cbKH.SelectedValue = i
    End Sub

    Private Sub cbChuaGiao_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbChuaGiao.CheckedChanged
        If cbChuaGiao.Checked = True Then
            load_DGV(" WHERE Tinhtrang = N'Chưa giao'")
        Else
            load_DGV("")
        End If
    End Sub

    Private Sub btnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAll.Click
        load_DGV("")
    End Sub

    Private Sub cbLoai_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLoai.SelectedIndexChanged
        If cbLoai.SelectedIndex <> 0 Then
            lbDVT.Visible = True
            Dim dt As New DataTable
            Dim cn As New DatabaseConnect
            dt = cn.GetData("select dvt.donvitinh from SanPham sp,SanPham_DonViTinh dvt where sp.donvitinh = dvt.donvitinhID and sp.MaLoai =" & cbLoai.SelectedValue & "")
            lbDVT.Text = dt.Rows(0)("donvitinh").ToString()
        End If
    End Sub

    Private Sub dgv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv.Click
        If dgv.SelectedRows.Count > 0 Then
            btnCN.Enabled = True
            dtpNgayGiao.Enabled = True
            'Load DGV Chitiet theo ma dat hang
            load_DGVCT("")
            lbMaDH.Text = dgv.SelectedRows(0).Cells(0).Value.ToString()
            Dim makh As String = dgv.SelectedRows(0).Cells(1).Value.ToString()
            Dim kh As New KhachHangBL
            Dim list As New List(Of KhachHangDT)
            list = kh.GetData(" WHERE MaKH = '" + makh + "'")
            lbtenKH.Text = list(0).TenKH
            dgvCT.Focus()
        Else
            load_DGVCT("")
        End If
    End Sub

    Private Sub dgvCT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvCT.Click
        If dgvCT.SelectedRows.Count > 0 Then
            If btnThem.Enabled = True Then
                gbTTDH.Enabled = False
            Else
                gbTTDH.Enabled = True
            End If
            btnOK.Enabled = False
            btnSua.Enabled = True
            btnXoa.Enabled = True
            cbLoai.Enabled = False
            cbLoai.SelectedValue = Convert.ToInt32(dgvCT.SelectedRows(0).Cells(1).Value)
            txtSL.Text = dgvCT.SelectedRows(0).Cells(2).Value.ToString()
            txtGhiChu.Text = dgvCT.SelectedRows(0).Cells(3).Value.ToString()
        End If
    End Sub

    Private Sub lbTong(ByVal thanhtien As Double, ByVal flag As String)
        Dim tinh As Double = Double.Parse(lbThanhtien.Text)
        If flag = "delete" Then
            tinh = tinh - thanhtien
        Else
            tinh = tinh + thanhtien
        End If
        lbThanhtien.Text = tinh.ToString()
    End Sub

    '---group box
    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If dgvCT.SelectedRows.Count > 0 Then
            If txtSL.Text <> "" Then
                thanhtien = tinhThanhTien()
                Dim stt As Integer = Convert.ToInt32(dgvCT.SelectedRows(0).Cells(1).Value)
                dhctDT = New DatHangChiTietDT(lbMaDH.Text, cbLoai.SelectedValue, stt, Double.Parse(txtSL.Text), txtGhiChu.Text, thanhtien)
                dhctBL = New DatHangChiTietBL
                dhctBL.InUpDHCT(dhctDT, False)
                MessageBox.Show("Cập nhật thành công")
                load_DGVCT("")
            Else
                MessageBox.Show("Chưa nhập số lượng")
            End If
        Else
            Return
        End If
        lbTong(thanhtien, "update")
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim response As MsgBoxResult = MsgBox("Bạn có muốn xóa chi tiết đặt hàng này?", MsgBoxStyle.YesNo, "Thông báo")
        If response = MsgBoxResult.Yes Then
            txtSL.Text = ""
            txtGhiChu.Text = ""
            Dim stt As Integer = Convert.ToInt32(dgvCT.SelectedRows(0).Cells(1).Value)
            dhctDT = New DatHangChiTietDT(lbMaDH.Text, stt)
            dhctBL = New DatHangChiTietBL
            dhctBL.DelDHCT(dhctDT)
            MessageBox.Show("Xóa thành công")
            load_DGVCT(" WHERE MaPhieu = '" + lbMaDH.Text + "'")
        Else
            Return
        End If
        lbTong(thanhtien, "delete")
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If txtSL.Text <> "" Or cbLoai.SelectedIndex <> 0 Then
            thanhtien = tinhThanhTien()
            dhctBL = New DatHangChiTietBL
            dhBL = New DatHangBL
            Dim stt As Integer = dhctBL.GetSTT(lbMaDH.Text)
            dhctDT = New DatHangChiTietDT(lbMaDH.Text, cbLoai.SelectedValue, stt, Convert.ToDouble(txtSL.Text), txtGhiChu.Text, thanhtien)
            dhctBL.InUpDHCT(dhctDT, True)
            MessageBox.Show("Thêm thành công")
            load_DGVCT(" WHERE MaPhieu = '" + lbMaDH.Text + "'")
            txtSL.Text = ""
            txtGhiChu.Text = ""
        Else
            MessageBox.Show("Chưa nhập số lượng")
        End If
        lbTong(thanhtien, "insert")
    End Sub
End Class